package Servlet;

import com.cyy.bean.cart;
import com.cyy.bean.collect;
import com.cyy.bean.history;
import com.cyy.dao.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@WebServlet("/saveProductToDb")
public class saveProductServelt extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String infos=request.getParameter("info");
        int type=Integer.parseInt(request.getParameter("type"));
        String href="";
        if(type==0)
        {
            href="login.jsp";
        }
        else if(type==1)
        {
            href="/showMyInfo";
        }
        else if(type==2)
        {
            href="/showProduct";
        }
        else if(type==3)
        {
            href="/showUserBuyHistory";
        }
        if(!infos.equals("")) {
            try {
                recordDao.updateDataAfterModifyProduct();
            } catch (SQLException | ClassNotFoundException throwables) {
                throwables.printStackTrace();
            }
            String[] infoList = infos.replace('"', ' ').split("\n");
            productDao productDao = new productDao();
            String[] products = null;
            for (int i = 0; i < infoList.length; i++) {
                products = infoList[i].split(",");
                for (int h = 0; h < products.length; h++) {
                    System.out.println(products[h]);
                }
                try {
                    productDao.updateProduct(products);
                } catch (SQLException | ClassNotFoundException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        request.getRequestDispatcher(href).forward(request,response);
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }
}
